package com.sjq.common.mq.utils;

import javax.jms.JMSException;


public class JavaMQClient {
	
	private JavaDefualtProducer _producer = null;
	
	private void init(String destinationName){
		_producer = new JavaDefualtProducer(destinationName);
		try {
			_producer.afterPropertiesSet();
		} catch (Exception e) {
			e.printStackTrace();
		}
		_producer.start();
	}
	
	private void distroy(){
		if(null != _producer)
		_producer.stop();
	}
	
	
	
	
	
	/**
	 * 发送队列主要方法，可以写成静态的，方便调用。无需spring就可以了
	 * @param msg
	 * @param destinationName
	 * @throws JMSException
	 */
	public void sendMessage(String msg,String destinationName) throws JMSException{
		this.init(destinationName);
		_producer.sendMessageToMQ(msg);
		this.distroy();
	}
	
	
	
	private JavaTopicProducer topicProducer = null;
	
	
	private void initTopicProducer(String destinationName){
		topicProducer = new JavaTopicProducer(destinationName);
		try {
			topicProducer.afterPropertiesSet();
		} catch (Exception e) {
			e.printStackTrace();
		}
		topicProducer.start();
	}
	
	private void distroyTopicProducer(){
		if(null != topicProducer)
			topicProducer.stop();
	}
	
	
	/**
	 * 发布/订阅模式------发布消息
	 * @param msg
	 * @param destinationName
	 * @throws JMSException
	 */
	public void sendMessageByPublish(String msg,String topicName) throws JMSException{
		this.initTopicProducer(topicName);
		topicProducer.sendMessageToMQ(msg);
		this.distroyTopicProducer();
	}
}
